import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class LC_429 {
    class Node {
        public int val;
        public List<Node> children;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    };
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> lists = new ArrayList<>();
        int size = 1;
        Queue<Node> que = new LinkedList<Node>();
        if(root == null){
            return lists;
        }
        que.offer(root);
        while(!que.isEmpty()){
            List<Integer> list = new ArrayList<>();
            int len = size;
            size = 0;
            while(len != 0){
                len--;
                Node node = que.poll();
                list.add(node.val);
                List<Node> childrens = node.children;
                if(childrens == null || childrens.size() == 0){
                    continue;
                }
                for(Node children : childrens){
                    que.offer(children);
                    size++;
                }
            }
            lists.add(list);
        }
        return lists;
    }
}
